pub mod backup;
pub mod code;
pub mod common;
pub mod file;
pub mod goods;
pub mod level;
pub mod node;
pub mod psn;
pub mod setting;
pub mod station;
pub mod stock;
pub mod task;
pub mod tech;
pub mod user;

use super::{
    err, err_value, json, ok, throw_err, Base, CSend, Client, Collect, ECode, Fbf, KeyNode, Lock,
    MapSv, RNull, Resp, SStr, SvCache, VBase, VMap, Value, VecS, VecSv, VecV,
};
use level::Level;

pub async fn doit(
    data: &Value,
    model: &str,
    cmd: &str,
    name: Option<&str>,
    client: &Client,
    cache: SvCache,
    lock: Lock,
    tx: CSend,
) -> Resp {
    match model {
        "auth" | "equip" | "pick" | "sidehand" | "back" | "order" | "sideauto" => {
            let v = common::Common;
            v.do_cmd(model, data, cmd, client, cache, lock).await
        }
        "backup" => {
            let v = backup::BackUp;
            v.do_cmd(data, cmd, client).await
        }
        "file" => {
            let n = name.ok_or(err_value(ECode::MissKey, "miss name"))?;

            let v = file::FileDb;
            v.do_cmd(data, cmd, &n, cache, lock).await
        }
        "field" => {
            let n = name.ok_or(err_value(ECode::MissKey, "miss name"))?;

            let v = setting::Field;
            v.do_cmd(data, cmd, &n, cache, lock).await
        }
        "setting" => {
            let v = setting::Setting;
            v.do_cmd(data, cmd, cache, lock).await
        }
        "code" => {
            let v = code::Code;
            v.do_cmd(data, cmd, client, cache, lock, tx).await
        }
        "cache" => {
            let v = node::Node;
            v.do_cache(data, cmd, cache).await
        }
        "node" => {
            let v = node::Node;
            v.do_cmd(data, cmd, client).await
        }
        "psn" => {
            let v = psn::Psn;
            v.do_cmd(data, cmd, client, cache, lock).await
        }
        "station" => {
            let v = station::Station;
            v.do_cmd(data, cmd, client, cache, lock, tx).await
        }
        "part" | "oil" | "buy" | "sale" | "stell" | "cera" | "cage" | "seal" | "money"
        | "inner" | "outer" | "forge" | "lathe" => {
            let v = stock::Stock;
            v.do_cmd(model, data, cmd, client, cache, lock).await
        }
        "task" => {
            let v = task::Task;
            v.do_cmd(data, cmd, client, cache, lock, tx).await
        }
        "goods" | "away" => {
            let v = goods::Goods;
            v.do_cmd(model, data, cmd, client, cache, lock).await
        }
        "tech" | "esop" | "bom" => {
            let v = tech::Tech;
            v.do_cmd(model, data, cmd, client, cache, lock).await
        }
        "user" => {
            let v = user::User;
            v.do_cmd(data, cmd, client, cache, lock).await
        }
        _ => throw_err!(ECode::NoServe, "no cmd"),
    }
}
